package response;

import builtin.SpecialPages;
import config.Config;
import request.Request;
import utils.FileUtils;

import java.io.IOException;

public class StaticResponse {
    public static Response response(Request request) {
        String resource = request.getResource();
        String webRoot = Config.getConfig().getWebRoot();
        Response response = new Response();

        response.setContentType("text/html");

        try {
            String fileContent = "";
            if (resource.equals("/")) {
                fileContent = SpecialPages.getPage(0);
            } else {
                String path = webRoot + resource;
//                System.out.println(path);
                fileContent = FileUtils.readFileToString(path);
            }

            response.setStatusCode(200);
            response.setContent(fileContent);
        } catch (IOException e) {
//            e.printStackTrace();

            response.setStatusCode(404);
            response.setContent(SpecialPages.getPage(404));
        }

        return response;
    }
}
